package com.hp.airmap.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;

import rx.Subscription;

/**
 * 日期：2016/11/13/013
 * 作者：胡鹏
 * 用途：基类Activity
 */

public class BaseActivity extends AppCompatActivity {

    protected static String TAG_LOG = null;// Log tag
    protected Context mContext;
    private Toast toast;
    private KProgressHUD progressDialog;
    private SubscriptionWrapper wrapper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = this;
        this.TAG_LOG = this.getClass().getSimpleName();
        this.toast = Toast.makeText(mContext, "", Toast.LENGTH_LONG);
        progressDialog = KProgressHUD.create(mContext)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f);
        this.wrapper=new SubscriptionWrapper();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        wrapper.cancelAll();
        wrapper=null;
        toast=null;
    }

    /**
     * 获取Application实例
     * @return
     */
    protected MyApp getApp(){
        return ((MyApp)getApplication());
    }

    /**
     * 显示键盘
     * @param view
     */
    protected void showKeyboard(View view){
        InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, 0);
    }

    /**
     * 隐藏键盘
     * @param view
     */
    protected void hideKeyboard(View view){
        InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 跳转到某个Activity
     * @param clazz
     */
    protected void go(Class<?> clazz) {
        go(clazz, null);
    }

    /**
     * 跳转到某个Activity 带参
     * @param clazz
     * @param data
     */
    protected void go(Class<?> clazz, Bundle data) {
        Intent intent = new Intent(mContext, clazz);
        if (data != null) {
            intent.putExtras(data);
        }
        startActivity(intent);
    }

    /**
     * 显示信息
     * @param msg
     */
    protected void showMessage(String msg) {
        toast.setText(msg);
        toast.show();
    }

    public void showLoading() {
        hideLoading();
        progressDialog.show();
    }

    //    @Override
    public void hideLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }

    }


    /**
     * 加入RxJava被观察对象,在onDestroy时会自动取消该对象的执行
     * @param subscription
     */
    protected void addSubscribe(Subscription subscription){
        wrapper.add(subscription);
    }


}
